<?php
namespace app\common\service;

use app\admin\Service;
use app\common\model\AppleDebtBacked;

class DeptBackedService extends Service
{

    public static function getPlanDept($order_id){
        $lst = [];
        $pay = [];
        $other = [];
        $jianmian = [];
        //还款记录
        $debtBacked = AppleDebtBacked::mk()->where(['order_id' => $order_id, 'status' => 1])->select();
        //账单类型
        $debtType = [1 => '还款计划', 2 => '押金', 3 => '意外保障金额', 4 => '溢价金', 5 => '买断尾款', 6 => '减免金额', 7 => '优惠金额', 8 => '运费金额', 9 => '违约金', 10 => '补充租金', 11 => '退款'];
        if(!empty($debtBacked)){
            foreach ($debtBacked as $vd){
                $planIndex = $vd['plan_index'];
                $amount = $vd['payment_amount'];
                if(!isset($pay[$planIndex])) $pay[$planIndex] = 0;
                if(!isset($other[$planIndex])) $other[$planIndex] = [];
                if(!isset($jianmian[$planIndex])) $jianmian[$planIndex] = 0;
                if(in_array($vd['bill_type'], [1, 4, 8, 9, 10])){
                    $pay[$planIndex] = isset($pay[$planIndex]) ? bcadd($pay[$planIndex], $amount, 2) :  $amount;
                    if($vd['credit_amount'] > 0){
                        //$pay[$planIndex] = bcadd($pay[$planIndex], $vd['credit_amount'], 2);
                        $jianmian[$planIndex] = bcadd($jianmian[$planIndex], $vd['credit_amount'], 2);
                        $other[$planIndex][] = $debtType[6] . '：' . $vd['credit_amount'];
                    }
                }
                if(in_array($vd['bill_type'], [7, 11])){
                    $pay[$planIndex] = isset($pay[$planIndex]) ? bcsub($pay[$planIndex], $amount, 2) :  0 - $amount;
                    $jianmian[$planIndex] = isset($jianmian[$planIndex]) ? bcadd($jianmian[$planIndex], $amount, 2) :  $amount;
                }
                if(!in_array($vd['bill_type'], [1, 2, 11])){
                    $other[$planIndex][] = $debtType[$vd['bill_type']] . '：' . $amount;
                }

            }
        }
        $lst['pay'] = $pay;
        $lst['jianmian'] = $jianmian;
        $lst['other'] = $other;
        return $lst;
    }
}